
// $(document).ready(function () {
//     $("#")
// })
//ajax
$("#tbl_user").bootstrapTable({
    url:"guihuan/all",
    contentType:'application/x-www-form-urlencoded',
    method:'POST',
    toolbar:'#toolbar',
    sidePagination:'server',
    pageNumber:1,//当前页
    pageSize:5,//每页条数
    pageList:[5, 10, 15, 20,'ALL'],
    dataType:'json',//返回数据的类型
    cache:false,
    pagination:true,//使用分页
    showRefresh:true,//右上角刷新按钮
    showColumns:true,//右上角,列,列表
    queryParams:function(params){
        params["jy_id"] = $("#username").val();
        params["ts_id"] = $("#riqi").val();
        console.log(params)
        return params;
    },
    classes:'table table-hover table-striped',
    columns:[{
        checkbox:true
    },{
        field:"jy_id",
        title:"借阅单号",
        align:"center"
    },{
        field:"tushu.tu_name",
        title:"图书名称",
        align:"center"
    },{
        field:"tushu.jieyue",
        title:"借阅价格",
        align:"center"
    },{
        field:"duzhe.dz_id",
        title:"借书卡号",
        align:"center"
    },{
        field:"duzhe.dz_name",
        title:"读者姓名",
        align:"center"
    },{
        field:"jy_jiechu",
        title:"借出日期",
        align:"center"
    },{
        field:'operation',
        title:'操作',
        align:'center',
        formatter:function (value,row,index) {
            var array = [
                '<a href="javascript:void(0)" onclick="toAdd(\'归还图书\',\'guihuan/getById?id='+row.jy_id+'\')"><span>归还</span></a>',
                ''
            ];
            return array.join('');
        }
    }]
});

var posunjia = 0;

function chang(i) {
    var posun = $(i).val();
    var shoujia = $("#chushou").val();
    $("#zong").val(parseFloat($("#zong").val()) - parseFloat(posunjia));
    posunjia = parseFloat(parseFloat(parseInt(shoujia) * parseInt(posun)) / 100);
    var zong =  $("#zong").val();
    zong = parseFloat(zong) + parseFloat(parseInt(parseFloat(shoujia) * parseInt(posun)) / 100);
    $("#zong").val(zong);
}

function gaibian(gai){
    var shifou = $(gai).prop("checked");
    if(shifou){
        $(gai).parents(".form-group").next().show();

    }else{
        $("#zong").val(parseFloat($("#zong").val()) - parseFloat(posunjia));
        posunjia= 0;
        $(gai).parents(".form-group").next().find("#fajin").val(-1);
        $(gai).parents(".form-group").next().hide();
    }

}

//删除一行数据
function delRow(row) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        $.ajax({
            url:'chushou/delete',
            method:"post",
            data:{"id":row},
            success:function (data) {
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');
            }
        });


    });
}


//显示模态框并打开新增界面
function toAdd(title,url) {
    $("#modal1").find(".modal-title").html(title);
    $("#modal1").find(".modal-body").load(url);
    $("#modal1").modal('show');
}

//提交表单
function submitForm(btn) {
    var yue=$("#dz_jine").val();
    var zong = $("#zong").val();

    if(yue >zong ){
        var shifou = $("#posun").prop("checked");
        if(shifou){
            var tu_id=$("#ts_id").val();
            var posun = $("#fajin").val();
            $.ajax({
                url:"guihuan/tushuup",
                method:"post",
                //data:str,
                //FormData(dom)
                data:{"ts_id":tu_id,"posun":posun},
                dataType:'json',
                // processData: false,
                // contentType: false,
                success:function (data) {

                }
            });
        }

        var du_id = $("#dz_id").val();
        var jine = parseFloat($("#dz_jine").val()) - parseFloat($("#zong").val());
        $.ajax({
            url:"guihuan/duzheup",
            method:"post",
            //data:str,
            //FormData(dom)
            data:{"dz_id":du_id,"dz_jine":jine},
            dataType:'json',
            // processData: false,
            // contentType: false,
            success:function (data) {

            }
        });




        // $("#cs_jine").removeAttr("disabled");
        // var $form = $(btn).parents("#modal1").find("#myForm");
        // $($form).find("#dz_shenFen").removeAttr("disabled");
        // var action = $form.attr("action");
        // var str = $form.serialize();
        // $.ajax({
        //     url:action,
        //     method:"post",
        //     //data:str,
        //     //FormData(dom)
        //     data:str,
        //     dataType:'json',
        //     // processData: false,
        //     // contentType: false,
        //     success:function (data) {
        //         if(data.result){
        //             swal("成功", "操作成功", "success");
        //             //隐藏模态框
        //             $("#modal1").modal('hide');
        //             //刷新表格
        //             $("#tbl_user").bootstrapTable('refresh');
        //
        //         }
        //     }
        // });
    }else{
        swal("错误", "余额不足快去充值吧~~", "error");
    }
}

//按条件查询
function setCondition() {
    $("#tbl_user").bootstrapTable('refresh');
}

//批量删除
function batchDel() {
    //1.判断是否有行被选中
    //getSelections
    //如果没有行被选中,返回[]
    var rows = $("#tbl_user").bootstrapTable("getSelections");
    if(rows.length == 0){
        swal("提示", "请选中要删除的行", "warning");
        return;
    }
    //2.1获取要删除的行(主键)  []
    //2.1获取要删除的行(主键)  []
    var model = {};
    for(var i = 0;i<rows.length;i++){
        model["ids["+i+"]"] = rows[i].cs_id
    }
//2.2传递数据到Controller
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        //ajax
        $.ajax({
            url:"chushou/batchDel",
            method:"post",
            data:model,
            dataType:'json',
            success:function (data) {
                if(data.result){
                    swal("添加成功！", "您已经永久添加了这些信息。", "success");
                    //隐藏模态框
                    $("#modal1").modal('hide');
                    //刷新表格
                    $("#tbl_user").bootstrapTable('refresh');

                }
            }
        });

    });
}

function batchAdd(title,url) {
    $("#modalExcel").find(".modal-title").html(title);
    $("#modalExcel").find(".modal-body").load(url);
    $("#modalExcel").modal('show');
}

function batchInsert() {
    var $form = $("#modalExcel").find('#myFormXls');
    var action = $form.attr('action');
    $.ajax({
        url:action,
        method:"post",
        data:new FormData($form.get(0)),
        dataType:'json',
        processData: false,
        contentType: false,
        success:function (data) {
            if(data.result){
                swal("添加成功！", "您已经永久添加了这些信息。", "success");
                //隐藏模态框
                $("#modalExcel").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

function exportExcel(url) {
    var form = document.createElement('form');
    form.setAttribute("action",url);
    form.setAttribute("method","post");
    document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}